<?php
/**
 * Created by PhpStorm.
 * User: Shinelon
 * Date: 2021/4/17
 * Time: 17:45
 */

namespace app\admin\controller;


use app\admin\model\OrderGoods;
use app\AdminRest;
use think\App;
use think\facade\View;
use app\admin\model\Order as OrderModel;

class Order extends AdminRest
{
    protected $model;

    public function __construct(App $app, OrderModel $model)
    {
        parent::__construct($app);
        $this->model = $model;
    }

    public function addOrder()
    {
        try {
            validate($this->model->rule, $this->model->message)->check($this->_param);
            $data = $this->_param;
            $data['create_time'] = $data['update_time'] = time();
            $this->model->save($data);
            return $this->success('添加成功');
        } catch (\Exception $exception) {
            return $this->error($exception->getMessage());
        }
    }

    public function orderList()
    {
        $needData = $this->_param['needData'] ?? 0;
        switch ($needData) {
            case  1:
                $keyword = '';
                if (isset($this->_param['keyword']) && !empty($this->_param['keyword']))
                    $keyword = $this->_param['keyword'];
                $where = ['deleted' => 0];
                if (isset($this->_param['status']) && !empty($this->_param['status']))
                    $where['status'] = $this->_param['status'];
                $orders = $this->model->when(!empty($keyword),
                    function ($q) use ($keyword) {
                        $q->where('name', 'like', "%$keyword%");
                    })
                    ->where($where)->order('id', 'desc')->paginate($this->_param['size'] ?? 10)->toArray();
                foreach ($orders['data'] as &$v) {
                    $v['pay_time'] = $v['pay_time'] > 0 ? date('Y-m-d H:i:s', $v['pay_time']) : '';
                }
                return $this->success($orders);
                break;
            default:
                return View::fetch('orderList');
                break;
        }
    }


    public function editOrder()
    {
        $data = $this->_param;
        unset($data['id']);
        if (empty($this->_param['id']) || $this->_param['id'] < 0)
            return $this->error('数据有误');
        try {
            validate($this->model->rule, $this->model->message)->check($data);
            $data['update_time'] = time();
            if (isset($data['deleted']) && $data['deleted'] == 1) {
                $data['delete_time'] = $data['update_time'];
            }
            unset($data['create_time']);
            $this->model->where(['id' => $this->_param['id']])->save($data);
            return $this->success('修改成功');
        } catch (\Exception $exception) {
            return $this->error($exception->getMessage());
        }
    }

    public function orderGoodsList(OrderGoods $model)
    {
        if (empty($this->_param['orderId']) || $this->_param['orderId'] < 0)
            return $this->error('数据有误');
        $data = $model->where(['order_id' => $this->_param['orderId']])->paginate($this->_param['size'] ?? 10)->toArray();
        return $this->success($data);
    }
}
